package com.jxwifi.cloud.quickclean.wxapi;


import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;

import com.idroid.utils.MToast;
import com.jxwifi.cloud.quickclean.app.ClientBasicActivity;
import com.jxwifi.cloud.quickclean.app.StateUtils;
import com.jxwifi.cloud.quickclean.pay.PayResult;
import com.tencent.mm.opensdk.constants.ConstantsAPI;
import com.tencent.mm.opensdk.modelbase.BaseReq;
import com.tencent.mm.opensdk.modelbase.BaseResp;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.IWXAPIEventHandler;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;

import org.simple.eventbus.EventBus;


/**
 * 用于接收微信支付结果回调
 */
public class WXPayEntryActivity extends ClientBasicActivity implements IWXAPIEventHandler {
    private IWXAPI api;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //setContentView(R.layout.activity_wx_pay_entry);
        api = WXAPIFactory.createWXAPI(this, StateUtils.WX_APPID);
        api.handleIntent(getIntent(), this);
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        setIntent(intent);
        api.handleIntent(intent, this);
    }

    @Override
    public void onReq(BaseReq req) {

    }


    /**
     * 得到支付结果回调
     */
    @Override
    public void onResp(final BaseResp resp) {
        Log.w("oushuhua", "demo onResp type = " + resp.getType());
        if (resp.getType() == ConstantsAPI.COMMAND_PAY_BY_WX) {
            Log.w("oushuhua", "onResp errCode = " + resp.errCode);
            if (resp.errCode==0){
                EventBus.getDefault().post(new PayResult(true, "支付成功"),"e-WXPayResult");
                overridePendingTransition(0,0); //这个也很重要
                finish();
            } if(resp.errCode==-2){
                MToast.shortToast(mBaseContext,"用户已取消");
                //Log.w("oushuhua", "用户已取消" + resp.errStr);
                overridePendingTransition(0,0); //这个也很重要
                finish();
            }else if(resp.errCode==-1){
                MToast.shortToast(mBaseContext,"支付失败  "+resp.errStr+"错误代码"+resp.errCode);
                //Log.w("oushuhua", "支付失败 = " + resp.errStr+"错误代码"+resp.errCode);
                overridePendingTransition(0,0); //这个也很重要
                finish();
            }
        }else{
            Log.w("oushuhua", "onResp errCode 不等于5 = " + resp.errCode);
        }
    }
}